/*
 * Copyright 2011 Helios Technologies.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.heliostech.gwt.sample.dialog.wizard.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.heliostech.gwt.widget.dialog.wizard.BaseWizard;
import com.heliostech.gwt.widget.dialog.wizard.IWizardStep;

public class BaseWizardSample implements EntryPoint {

	public interface Resources extends ClientBundle {
		@Source("./step-1-icon.png")
		ImageResource step1Icon();
		@Source("./step-2-icon.png")
		ImageResource step2Icon();
		@Source("./step-3-icon.png")
		ImageResource step3Icon();
	}
	
	private Resources resources = GWT.create(Resources.class);
	private BaseWizard myWizard;
	private TextBox fNameF;
	private TextBox lNameF;
	private TextBox addressF;
	private TextBox pCodeF;
	private TextBox cityF;
	private TextBox countryF;
	
	private TextBox logNameF;
	private TextBox phoneF;
	private TextBox emailF;
	private ListBox typeF;
	
	@Override
	public void onModuleLoad() {
		myWizard = new BaseWizard("Registration Wizard", true);
		myWizard.setSize("42em", "20em");
		myWizard.addStep(0, getStep1());
		myWizard.addStep(1, getStep2());
		myWizard.addStep(2, getStep3());
		
		myWizard.center();
	}
	
	public IWizardStep getStep1() {
		IWizardStep s1 = new IWizardStep() {
			
			private IsWidget content;
			
			@Override
			public boolean onBeforePrevious() {
				return true;
			}
			
			@Override
			public boolean onBeforeNext() {
				if(fNameF.getValue()!=null && fNameF.getValue().length() > 0) {
					return true;
				} else {
					myWizard.showError("First name is mandatory.");
					return false;
				}
			}
			
			@Override
			public boolean onBeforeFinish() {
				return true;
			}
			
			@Override
			public String getStepAction() {
				return "First Step: Personal Information";
			}

			@Override
			public String getStepDescription() {
				return "Provide your personal information and click on the [Next] button.";
			}
			
			@Override
			public ImageResource getStepIcon() {
				return resources.step1Icon();
			}
			
			@Override
			public IsWidget getStepContent() {
				if (content == null) {
					// Create a table to layout the form options
				    FlexTable layout = new FlexTable();
				    layout.setCellSpacing(6);
				    
				    // First row
				    layout.setHTML(0, 0, "First Name");
				    fNameF = new TextBox();
				    layout.setWidget(0, 1, fNameF);
				    layout.setHTML(0, 2, "Last Name");
				    lNameF = new TextBox();
				    layout.setWidget(0, 3, lNameF);
				    // Second row
				    layout.setHTML(1, 0, "Address");
				    addressF = new TextBox();
				    layout.setWidget(1, 1, addressF);
				    layout.setHTML(1, 2, "Postal Code");
				    pCodeF = new TextBox();
				    layout.setWidget(1, 3, pCodeF);
				    // third row etc...
				    layout.setHTML(2, 0, "City");
				    cityF = new TextBox();
				    layout.setWidget(2, 1, cityF);
				    layout.setHTML(2, 2, "Country");
				    countryF = new TextBox();
				    layout.setWidget(2, 3, countryF);
				    content = layout;
				}
			    return content;
			}
			
			@Override
			public int getIndex() {
				return 0;
			}
			
			@Override
			public void cleanup() {
				fNameF.setValue("");
				lNameF.setValue("");
				addressF.setValue("");
				pCodeF.setValue("");
				cityF.setValue("");
				countryF.setValue("");
			}
			
			@Override
			public boolean allowPrevious() {
				return false;
			}
			
			@Override
			public boolean allowNext() {
				return true;
			}
			
			@Override
			public boolean allowFinish() {
				return false;
			}
		};
		return s1;
	}
	
	public IWizardStep getStep2() {
		IWizardStep s2 = new IWizardStep() {
			
			private IsWidget content;
			
			@Override
			public boolean onBeforePrevious() {
				return true;
			}
			
			@Override
			public boolean onBeforeNext() {
				return true;
			}
			
			@Override
			public boolean onBeforeFinish() {
				return false;
			}
			
			@Override
			public String getStepAction() {
				return "Second Step: Login name and contact information";
			}
			
			@Override
			public String getStepDescription() {
				return "Choose your login name and provide your contact information than click on the [Next] button.";
			}
			
			@Override
			public ImageResource getStepIcon() {
				return resources.step2Icon();
			}
			
			@Override
			public IsWidget getStepContent() {
				if (content == null) {
					// Create a table to layout the form options
				    FlexTable layout = new FlexTable();
				    layout.setCellSpacing(6);
				    
				    // First row
				    layout.setHTML(0, 0, "Login Name");
				    logNameF = new TextBox();
				    layout.setWidget(0, 1, logNameF);
				    layout.setHTML(0, 2, "Phone");
				    phoneF = new TextBox();
				    layout.setWidget(0, 3, phoneF);
				    // Second row
				    layout.setHTML(1, 0, "Email");
				    emailF = new TextBox();
				    layout.setWidget(1, 1, emailF);
				    layout.setHTML(1, 2, "Contact me by:");
				    typeF = new ListBox();
				    typeF.addItem("Select...");
				    typeF.addItem("Phone");
				    typeF.addItem("Email");
				    layout.setWidget(1, 3, typeF);
				    content = layout;
				}
			    return content;
			}

			@Override
			public int getIndex() {
				return 1;
			}
			
			@Override
			public void cleanup() {
				logNameF.setValue("");
				phoneF.setValue("");
				emailF.setValue("");
				typeF.setSelectedIndex(0);
			}
			
			@Override
			public boolean allowPrevious() {
				return true;
			}
			
			@Override
			public boolean allowNext() {
				return true;
			}
			
			@Override
			public boolean allowFinish() {
				return false;
			}
		};
		return s2;
	}
	
	public IWizardStep getStep3() {
		IWizardStep s3 = new IWizardStep() {
			
			@Override
			public boolean onBeforePrevious() {
				return true;
			}
			
			@Override
			public boolean onBeforeNext() {
				return false;
			}
			
			@Override
			public boolean onBeforeFinish() {
				return false;
			}
			
			@Override
			public String getStepAction() {
				return "Last Step: Review your information";
			}
			
			@Override
			public String getStepDescription() {
				return "Review the information you provided below to make sure everything is correct and click the [Finish] button to create your account.";
			}
			
			@Override
			public ImageResource getStepIcon() {
				return resources.step3Icon();
			}
			
			@Override
			public IsWidget getStepContent() {
				// Create a table to layout the form options
			    FlexTable layout = new FlexTable();
			    layout.setCellSpacing(6);
			    
			    // row
			    layout.setHTML(0, 0, "First Name");
			    layout.setText(0, 1, fNameF.getValue());
			    layout.setHTML(0, 2, "Last Name");
			    layout.setText(0, 3, lNameF.getValue());
			    // row
			    layout.setHTML(1, 0, "Address");
			    layout.setText(1, 1, addressF.getValue());
			    layout.setHTML(1, 2, "Postal Code");
			    layout.setText(1, 3, pCodeF.getValue());
			    // row
			    layout.setHTML(2, 0, "City");
			    layout.setText(2, 1, cityF.getText());
			    layout.setHTML(2, 2, "Country");
			    layout.setText(2, 3, countryF.getText());
			    // row
			    layout.setHTML(3, 0, "Login Name");
			    layout.setText(3, 1, logNameF.getValue());
			    layout.setHTML(3, 2, "Phone");
			    layout.setText(3, 3, phoneF.getValue());
			    // row
			    layout.setHTML(4, 0, "Email");
			    layout.setText(4, 1, emailF.getValue());
			    layout.setHTML(4, 2, "Contact me by:");
			    layout.setText(4, 3, typeF.getValue(typeF.getSelectedIndex()));
			    return layout;
			}

			@Override
			public int getIndex() {
				return 2;
			}
			
			@Override
			public void cleanup() {

			}
			
			@Override
			public boolean allowPrevious() {
				return true;
			}
			
			@Override
			public boolean allowNext() {
				return false;
			}
			
			@Override
			public boolean allowFinish() {
				return true;
			}
		};
		return s3;
	}
}	
